import java.util.Arrays;


public class Test2 {
    public static void main(String[] args) {
        //填充数组
        int[] array = new int[10];
        Arrays.fill(array,0,6,1);//[0,6)
        System.out.println(Arrays.toString(array));
    }
    public static void main7(String[] args) {
        //数组排序--冒泡排序
        int[] array = {2,6,9,3,1,4,5,7,8,10};
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
    }
    /*
    Arrays.binarySearch(数组名，要查找的元素）
     */
    public static void main6(String[] args) {
        int[] array1 = {1,2,3,4,5,6,7,8,9,10};
        int ret = Arrays.binarySearch(array1,7);//找不到返回负值
        System.out.println(ret);
    }
    public static void main5(String[] args) {
        int[] array1 = {1,2,3,4,5,6,7,8,9,10};
        int[] copy = copy(array1);
        System.out.println(Arrays.toString(copy));
        System.out.println("===============================");
       int[]  copy2 = Arrays.copyOf(array1,array1.length);
        System.out.println(Arrays.toString(copy2));
        int[]  copy3 = Arrays.copyOf(array1,2*array1.length);//扩容复制
        System.out.println(Arrays.toString(copy3));
        int[]  copy4 = Arrays.copyOfRange(array1,2,6);//部分复制，但[2,6)
        System.out.println(Arrays.toString(copy4));
    }
   public static int[] copy(int[] array1) {
       int[] copy = new int[array1.length];
       for (int i = 0; i < array1.length; i++) {
           copy[i] = array1[i];
       }
       return copy;
   }
    public static void main4(String[] args) {
        int[] array1 = {1,2,3,4,5,6,7,8,9,10};
        int[][] array2 = {{1,3,5,7,9},{2,4,6,8,10}};
        print(array1);
        for (int i = 0; i < array1.length; i++) {
            System.out.print(array1[i] +" ");
        }
        System.out.println();
        for (int x:
             array1) {
            System.out.print(x +" ");
        }
        System.out.println();
        System.out.println(Arrays.toString(array1));
        System.out.println();

        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 5; j++) {
                System.out.print(array2[i][j] +" ");
            }
            System.out.println();
        }
        for (int i = 0; i < array2.length; i++) {
            for (int j = 0; j < array2[i].length; j++) {
                System.out.print(array2[i][j] +" ");
            }
            System.out.println();
        }
        System.out.println(Arrays.deepToString(array2));

    }
    public static void print(int[] array1) {
        for (int i = 0; i < array1.length; i++) {
            System.out.print(array1[i] +" ");
        }
        System.out.println();
    }

    //实现二分查找
    public static int myBinarySearch(int[] array,int k) {
        int ret = 0;
        int left = 0;
        int right = array.length-1;
        while(left <= right) {
            int mid = (right-left)/2 + left;
            if(k < array[mid]) {
                right = mid - 1;
            }else if(k > array[mid]) {
                left = mid + 1;
            }else {
                ret = mid;
                break;
            }
        }
     return ret;
    }
    public static void main3(String[] args) {
        int[] array = {2,4,6,7,8,9,17};
        int k = 6;
        int ret = myBinarySearch(array,k);
        System.out.println(ret);
    }

    //实现整型数组转化为字符串的方法
    public static String myToString(int[] array) {
        String str = "[";
        for (int i = 0; i < array.length; i++) {
            if(i != array.length-1) {
                str = str + array[i] +"," +" ";
            }else {
                str += array[array.length-1] +"]";
            }
        }
        return str;
    }
    public static void main2(String[] args) {
        int[] array = {1,2,3,4};
        String ret = myToString(array);
        System.out.println(ret);
    }
    public static void main1(String[] args) {
        int[] array1 = {1,2,3,4,5};
        int[] array2 = new int[]{2,3,4,5,6};
        int[] array3 = new int[5];
        for (int i = 0; i < array1.length; i++) {
            System.out.print(array1[i] +" ");
        }
        System.out.println();
        for (int x : array1) {
            System.out.println(array1);
        }
        String ret = Arrays.toString(array1);
    }
}
